package cn.tiga.org.controller;

import cn.tiga.basic.exception.BusinessExeption;
import cn.tiga.basic.util.ExcelUtil;
import cn.tiga.basic.util.JsonResult;
import cn.tiga.basic.util.PageList;
import cn.tiga.org.domain.ShopAuditLog;
import cn.tiga.org.service.IShopService;
import cn.tiga.org.domain.Shop;
import cn.tiga.org.query.ShopQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Shop> list() {

        return shopService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.pageQuery(query);
    }

    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessExeption e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");

        }
    }

    /**
     * @Title: pass 通过后的操作
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/26 21:39
     * @Parameters: [log]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PostMapping("/audit/pass")
    public JsonResult pass(@RequestBody ShopAuditLog log) {
        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }

    /**
     * 驳回
     *
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "驳回")
    public JsonResult reject(@RequestBody ShopAuditLog log) {

        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }

    /**
     * @Title: 文件导入接口
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/27 0:22
     * @Parameters: [response]
     * @Return void
     */

    @GetMapping("/export/excel")
    public void export(HttpServletResponse response) {
        try {
            List<Shop> shops = shopService.loadAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Title: 文件导出接口
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/5 18:35
     * @Parameters: []
     * @Return void
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    /**
     * @Title: 获取前端表格数据
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date:  2022/7/5 19:09
     * @Parameters:
     * @Return
     */
    @GetMapping("/echarts")
    public Map<String,Object> loadShopEchartsData(){
        return shopService.loadShopEchartsData();
    }

}
